package com.xq.shop.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xq.shop.config.FileConfig;
import com.xq.shop.config.Redis;
import com.xq.shop.dao.domain.entity.*;
import com.xq.shop.dao.mapper.OrderMapper;
import com.xq.shop.dto.*;
import com.xq.shop.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xq.shop.utils.MyConstants;
import com.xq.shop.vo.*;
import enums.DataStatus;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import utils.BaseConstants;
import utils.BasePage;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author gaoxiaojin
 * @since 2024-07-30
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Resource
    private FileConfig fileConfig;

    private final IOrderGoodsService orderGoodsService;
    private final IFlowService flowService;
    private final IOrderOtherExpensesService orderOtherExpensesService;
    private final IOrderTrendsService orderTrendsService;
    private final IOrderGoodsProcureService orderGoodsProcureService;
    private final IOrderRefundService orderRefundService;
    private final IWebUserService webUserService;
    private final IOrderGoodsDeliveryService orderGoodsDeliveryService;
    private final IOrderGoodsImageService orderGoodsImageService;

    @Resource
    private Redis redis;

    @Autowired
    public OrderServiceImpl(IOrderGoodsService orderGoodsService, IFlowService flowService, IOrderOtherExpensesService orderOtherExpensesService,
                            IOrderTrendsService orderTrendsService, IOrderGoodsProcureService orderGoodsProcureService,
                            IOrderRefundService orderRefundService, IWebUserService webUserService, IOrderGoodsDeliveryService orderGoodsDeliveryService, IOrderGoodsImageService orderGoodsImageService) {
        this.orderGoodsService = orderGoodsService;
        this.flowService = flowService;
        this.orderOtherExpensesService = orderOtherExpensesService;
        this.orderTrendsService = orderTrendsService;
        this.orderGoodsProcureService = orderGoodsProcureService;
        this.orderRefundService = orderRefundService;
        this.webUserService = webUserService;
        this.orderGoodsDeliveryService = orderGoodsDeliveryService;
        this.orderGoodsImageService = orderGoodsImageService;
    }

    @Override
    public BasePage<OrderVo> pageQuery(QueryOrderDto dto) {
        IPage<OrderVo> page = baseMapper.page(new Page<>(dto.getPage(), dto.getLimit()), dto);
        List<OrderVo> orderVoList = page.getRecords();
        if (orderVoList.isEmpty()) {
            return new BasePage<OrderVo>().initPage(dto.getPage(), dto.getLimit(), page.getTotal(), page.getPages(), new ArrayList<>());
        }
        return new BasePage<OrderVo>().initPage(dto.getPage(), dto.getLimit(), page.getTotal(), page.getPages(), orderVoList);
    }

    @Override
    public OrderDetailVo detail(IntegerDto dto) {
        OrderDetailVo vo = baseMapper.detail(dto);
        List<OrderGoodsImageVo> goodsImageList = orderGoodsImageService.selectOrderGoodsImageByOrderId(dto);
        List<OrderGoodsVo> orderGoodsVoList = orderGoodsService.getOrderGoodsById(dto);
        for(OrderGoodsVo goodsVo:orderGoodsVoList) {
            List<OrderGoodsImageVo> imgList = new ArrayList<>();
            for(OrderGoodsImageVo gi:goodsImageList) {
                if(gi.getGoodsId().equals(goodsVo.getId())) {
                    gi.setUrl(fileConfig.montageUrl(gi.getUrl()));
                    imgList.add(gi);
                }
            }
            goodsVo.setImgList(imgList);
        }
        Date quotationTime = null;
        Date payTime = null;
        Date shipmentTime = null;
        Date completedTime = null;
        Date cancelTime = null;
        Map<Object, List<OrderGoodsVo>> map = orderGoodsVoList.stream().collect(Collectors.groupingBy(orderGoodsVo -> new CombinedKey(orderGoodsVo.getStoreName(), orderGoodsVo.getPlatform())));
        List<OrderOtherExpensesVo> orderOtherExpensesVoList = orderOtherExpensesService.getOrderOtherExpensesById(dto);
        List<OrderTrendsVo> orderTrendsVoList = orderTrendsService.queryTrendByOrderId(dto);
        for (OrderTrendsVo orderTrendsVo : orderTrendsVoList) {
            if (Objects.nonNull(orderTrendsVo.getMark())) {
                if (orderTrendsVo.getMark() == 2&&quotationTime==null) {
                    quotationTime = orderTrendsVo.getCreateTime();
                }
                if (orderTrendsVo.getMark() == 3&&payTime==null) {
                    payTime = orderTrendsVo.getCreateTime();
                }
                if (orderTrendsVo.getMark() == 4&&shipmentTime==null) {
                    shipmentTime = orderTrendsVo.getCreateTime();
                }
                if (orderTrendsVo.getMark() == 100&&completedTime==null) {
                    completedTime = orderTrendsVo.getCreateTime();
                }
                if (orderTrendsVo.getMark() == 101&&cancelTime==null) {
                    cancelTime = orderTrendsVo.getCreateTime();
                }
            }
        }
        vo.setQuotationTime(quotationTime);
        vo.setPayTime(payTime);
        vo.setShipmentTime(shipmentTime);
        vo.setCompletedTime(completedTime);
        vo.setCancelTime(cancelTime);
        vo.setOrderGoodsVoList(map);
        vo.setOrderOtherExpensesVoList(orderOtherExpensesVoList);
        vo.setOrderTrendsVoList(orderTrendsVoList);
        orderTrendsService.readTrendByOrderIdOfXq(dto);
        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editLabel(EditLabelDto dto) {
        Order order = getById(dto.getId());
        order.setLabel(dto.getLabel());
        updateById(order);
    }

    @Override
    public List<GoodsQuotationVo> goodsQuotationHistory(IntegerDto dto) {
        List<OrderGoods> orderGoods = orderGoodsService.getGoodsQuotationHistory(dto);
        return beanToVo(orderGoods);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void goodsQuotation(QuotationDto dto, Integer userId) {
        BigDecimal goodsTotal = BigDecimal.ZERO;
        BigDecimal domesticFreight = BigDecimal.ZERO;
        BigDecimal interFreight = BigDecimal.ZERO;
        List<OrderGoods> orderGoodsList = new ArrayList<>();
        BigDecimal rate = BigDecimal.ZERO;
        if (dto.getRate()!=null) {
            rate = dto.getRate();
        }
        List<OrderOtherExpenses> orderOtherExpensesList = new ArrayList<>();
        for (GoodsQuotationDto goodsQuotationDto:dto.getGoodsQuotationDtoList()) {
            OrderGoods orderGoods = orderGoodsService.getById(goodsQuotationDto.getId());
            BeanUtils.copyProperties(goodsQuotationDto, orderGoods);
            if (goodsQuotationDto.getQuotation()==null) {
                orderGoods.setQuotation(BigDecimal.ZERO);
            }
            if (goodsQuotationDto.getDomesticFreight()==null) {
                orderGoods.setDomesticFreight(BigDecimal.ZERO);
            }
            if (goodsQuotationDto.getForeignFreight()==null) {
                orderGoods.setForeignFreight(BigDecimal.ZERO);
            }
            orderGoodsList.add(orderGoods);
            goodsTotal = goodsTotal.add(orderGoods.getQuotation().multiply(new BigDecimal(orderGoods.getNumber())));
            domesticFreight = domesticFreight.add(orderGoods.getDomesticFreight());
            interFreight = interFreight.add(orderGoods.getForeignFreight());
        }
        Map<String, Object> map = new HashMap<>();
        map.put("order_id", dto.getOrderId());
        orderOtherExpensesService.removeByMap(map);
        BigDecimal otherTotal = BigDecimal.ZERO;
        for (OrderOtherExpensesDto orderOtherExpensesDto:dto.getOrderOtherExpensesDtoList()) {
            OrderOtherExpenses orderOtherExpenses = new OrderOtherExpenses();
            BeanUtils.copyProperties(orderOtherExpensesDto, orderOtherExpenses);
            orderOtherExpenses.setOrderId(dto.getOrderId());
            orderOtherExpenses.setCreateUser(userId);
            orderOtherExpensesList.add(orderOtherExpenses);
            otherTotal = otherTotal.add(orderOtherExpenses.getAmount());
        }
        BigDecimal orderTotal = goodsTotal.add(domesticFreight).add(interFreight).add(otherTotal);
        BigDecimal chargeTrouble = orderTotal.multiply(rate.divide(new BigDecimal(100)));
        orderTotal = orderTotal.add(chargeTrouble);
        Order order = getById(dto.getOrderId());
        order.setMark(2);
        order.setRate(rate);
        order.setChargeTrouble(chargeTrouble.setScale(2,BigDecimal.ROUND_HALF_UP));
        order.setOrderTotal(orderTotal.setScale(2,BigDecimal.ROUND_HALF_UP));
        order.setGoodsTotal(goodsTotal.setScale(2,BigDecimal.ROUND_HALF_UP));
        order.setDomesticFreight(domesticFreight.setScale(2,BigDecimal.ROUND_HALF_UP));
        order.setInterFreight(interFreight.setScale(2,BigDecimal.ROUND_HALF_UP));
        order.setOtherTotal(otherTotal.setScale(2,BigDecimal.ROUND_HALF_UP));

        OrderTrends orderTrends = new OrderTrends();
        orderTrends.setOrderId(dto.getOrderId());
        orderTrends.setMark(2);
        orderTrends.setType(2);
        orderTrends.setCreateUser(userId);
        orderTrendsService.save(orderTrends);
        updateById(order);
        orderGoodsService.updateBatchById(orderGoodsList);
        orderOtherExpensesService.saveBatch(orderOtherExpensesList);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editOrder(EditOrderDto dto, Integer id) {
        Order order = getById(dto.getId());
        BeanUtils.copyProperties(dto, order);
        order.setUpdateUser(id);
        updateById(order);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void sendMessage(OrderTrendsDto dto, Integer id) {
        OrderTrends orderTrends = new OrderTrends();
        orderTrends.setOrderId(dto.getId());
        orderTrends.setType(2);
        orderTrends.setContent(dto.getContent());
        orderTrends.setCreateUser(id);
        orderTrendsService.save(orderTrends);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeMessage(IntegerDto dto, Integer id) {
        OrderTrends orderTrends = orderTrendsService.getById(dto.getValue());
        orderTrends.setStatus(DataStatus.INVALID.getCode());
        orderTrendsService.updateById(orderTrends);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void goodsProcure(GoodsProcureDto dto, Integer userId) {
        OrderGoodsProcure orderGoodsProcure = new OrderGoodsProcure();
        if (dto.getId().equals(0)) {
            orderGoodsProcure.setCreateUser(userId);
            BeanUtils.copyProperties(dto, orderGoodsProcure);
            orderGoodsProcureService.save(orderGoodsProcure);
        } else {
            orderGoodsProcure = orderGoodsProcureService.getById(dto.getId());
            BeanUtils.copyProperties(dto, orderGoodsProcure);
            orderGoodsProcure.setUpdateUser(userId);
            orderGoodsProcureService.updateById(orderGoodsProcure);
        }
    }

    @Override
    public Map<String, List<GoodsProcureVo>> getGoodsProcure(StringDto dto) {
        List<GoodsProcureVo> voList = baseMapper.getGoodsProcureByOrderId(dto.getValue());
        Map<String, List<GoodsProcureVo>> map = voList.stream().collect(Collectors.groupingBy(GoodsProcureVo::getStoreName));
        if (voList.isEmpty()) {
            return new HashMap<>();
        }
        return map;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeGoodsProcure(IntegerDto dto) {
        OrderGoodsProcure orderGoodsProcure = orderGoodsProcureService.getById(dto.getValue());
        orderGoodsProcure.setStatus(DataStatus.INVALID.getCode());
        orderGoodsProcureService.updateById(orderGoodsProcure);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void refund(RefundDto dto, Integer userId) {
        OrderRefund orderRefund = new OrderRefund();
        BeanUtils.copyProperties(dto, orderRefund);
        orderRefund.setCreateUser(userId);
        orderRefundService.save(orderRefund);
        Order order = getById(dto.getOrderId());
        LambdaQueryWrapper<WebUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WebUser::getStatus, DataStatus.NORMAL.getCode());
        wrapper.eq(WebUser::getUuid, order.getUuid());
        WebUser webUser = webUserService.getOne(wrapper);
        webUser.setBalance(webUser.getBalance().add(dto.getAmount()));
        webUserService.updateById(webUser);
        Set<String> strings = redis.existsLike2(webUser.getId() + "web");
        if (!strings.isEmpty()) {
            String key = (String) strings.toArray()[0];
            JSONObject webUserVo = (JSONObject) redis.get(key);
            webUserVo.put("balance",webUser.getBalance());
            redis.set(key, webUserVo);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeRefund(IntegerDto dto) {
        OrderRefund orderRefund = orderRefundService.getById(dto.getValue());
        orderRefund.setStatus(DataStatus.INVALID.getCode());
        orderRefundService.updateById(orderRefund);
        Order order = getById(orderRefund.getOrderId());
        LambdaQueryWrapper<WebUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WebUser::getStatus, DataStatus.NORMAL.getCode());
        wrapper.eq(WebUser::getUuid, order.getUuid());
        WebUser webUser = webUserService.getOne(wrapper);
        webUser.setBalance(webUser.getBalance().subtract(orderRefund.getAmount()));
        webUserService.updateById(webUser);
        Set<String> strings = redis.existsLike2(webUser.getId() + "web");
        if (!strings.isEmpty()) {
            String key = (String) strings.toArray()[0];
            JSONObject webUserVo = (JSONObject) redis.get(key);
            webUserVo.put("balance",webUser.getBalance());
            redis.set(key, webUserVo);
        }
    }

    @Override
    public List<RefundVo> queryRefund(IntegerDto dto) {
        LambdaQueryWrapper<OrderRefund> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderRefund::getStatus, DataStatus.NORMAL.getCode());
        wrapper.eq(OrderRefund::getOrderId, dto.getValue());
        List<OrderRefund> orderRefunds = orderRefundService.list(wrapper);
        List<RefundVo> voList = new ArrayList<>();
        orderRefunds.forEach(of -> {
            RefundVo vo = new RefundVo();
            BeanUtils.copyProperties(of, vo);
            voList.add(vo);
        });
        return voList;
    }

    @Override
    public List<GoodsDeliveryVo> queryDelivery(StringDto dto) {
        LambdaQueryWrapper<OrderGoodsDelivery> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderGoodsDelivery::getStatus, DataStatus.NORMAL.getCode());
        wrapper.eq(OrderGoodsDelivery::getOrderNumber, dto.getValue());
        List<OrderGoodsDelivery> orderGoodsDeliveries = orderGoodsDeliveryService.list(wrapper);
        List<GoodsDeliveryVo> voList = new ArrayList<>();
        orderGoodsDeliveries.forEach(of -> {
            GoodsDeliveryVo vo = new GoodsDeliveryVo();
            BeanUtils.copyProperties(of, vo);
            OrderGoods orderGoods = orderGoodsService.getById(of.getOrderGoodsId());
            vo.setGoodsName(orderGoods.getName());
            vo.setSpecContent(orderGoods.getSpecContent());
            voList.add(vo);
        });

        return voList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delivery(DeliveryDto dto, Integer id) {
        List<OrderGoodsDelivery> orderGoodsDeliveries = new ArrayList<>();
        dto.getDeliveryGoodsDtoList().forEach(d -> {
            OrderGoodsDelivery orderGoodsDelivery = new OrderGoodsDelivery();
            orderGoodsDelivery.setNumber(d.getNumber());
            orderGoodsDelivery.setOrderGoodsId(d.getId());
            orderGoodsDelivery.setLogisticsName(dto.getLogisticsName());
            orderGoodsDelivery.setOrderNumber(dto.getOrderNumber());
            orderGoodsDelivery.setLogisticsNumber(dto.getLogisticsNumber());
            orderGoodsDelivery.setCreateUser(id);
            orderGoodsDeliveries.add(orderGoodsDelivery);
        });
        orderGoodsDeliveryService.saveBatch(orderGoodsDeliveries);
        if (dto.getMark() == 1) {
            LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Order::getStatus, DataStatus.NORMAL.getCode());
            wrapper.eq(Order::getOrderNumber, dto.getOrderNumber());
            Order order = getOne(wrapper);
            order.setMark(4);
            updateById(order);
            OrderTrends orderTrends = new OrderTrends();
            orderTrends.setCreateUser(id);
            orderTrends.setType(2);
            orderTrends.setMark(4);
            orderTrends.setOrderId(order.getId());
            orderTrendsService.save(orderTrends);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeDelivery(IntegerDto dto, Integer id) {
        OrderGoodsDelivery orderGoodsDelivery = orderGoodsDeliveryService.getById(dto.getValue());
        orderGoodsDelivery.setStatus(DataStatus.INVALID.getCode());
        orderGoodsDelivery.setUpdateUser(id);
        orderGoodsDeliveryService.updateById(orderGoodsDelivery);
    }

    @Override
    public HomeDataVo getData() {
        return baseMapper.getHomeData();
    }

    @Override
    public List<BigDecimal> getOrderCount(IntegerDto dto) {
        List<BigDecimal> countList = new ArrayList<>();
        Date today = new Date();
        Date time;
        if (dto.getValue() == 1) {
            time = MyConstants.getTodayTime();
        } else {
            time = MyConstants.getDayTime(dto.getValue());
        }
        List<OrderSalesVolumeVo> orderList = baseMapper.getOrderSalesVolume(time, null);
        int maxLength = dto.getValue()==1?24:MyConstants.daysBetween(time,today);
        DateFormat sdf = new SimpleDateFormat(dto.getValue() == 1?"yyyy-MM-dd HH":"yyyy-MM-dd");
        for (int i = 0; i < maxLength; i++) {
            Calendar d = Calendar.getInstance();
            d.setTime(time);
            int n = dto.getValue() == 1? i: (i+1);
            d.add(dto.getValue() == 1?Calendar.HOUR_OF_DAY:Calendar.DAY_OF_MONTH,n);
            String currentTime = sdf.format(d.getTime());
            BigDecimal amount = BigDecimal.ZERO;
            for(OrderSalesVolumeVo vo:orderList) {
                if (currentTime.equals(sdf.format(vo.getPayTime()))) {
                    amount = amount.add(vo.getOrderTotal());
                }
            }
            countList.add(amount.setScale(2,BigDecimal.ROUND_HALF_UP));
        }
        return countList;
    }

    @Override
    public Map<String, List<Integer>> getPvOrUv() {
        LambdaQueryWrapper<Flow> wrapper = new LambdaQueryWrapper<>();
        wrapper.gt(Flow::getCreateTime, MyConstants.getTodayTime());
        List<Flow> flowList = flowService.list(wrapper);
        Map<String, List<Integer>> map = new HashMap<>(8);
        List<Integer> pv = new ArrayList<>();
        List<Integer> uv = new ArrayList<>();
        if (flowList.isEmpty()) {
            for (int i = 0; i < 24; i++) {
                pv.add(0);
                uv.add(0);
            }
        } else {
            Map<Integer, List<Flow>> ordersPerHour = flowList.stream()
                    .collect(Collectors.groupingBy(order -> order.getCreateTime().getHours()));
            Map<Integer, List<Flow>> completeOrdersPerHour = IntStream.rangeClosed(0, 23)
                    .boxed()
                    .collect(Collectors.toMap(hour -> hour, hour -> ordersPerHour.getOrDefault(hour, new ArrayList<>())));
            completeOrdersPerHour.forEach((key, value) -> {
                if (value.size() > 0) {
                    pv.add(value.get(0).getPv());
                    uv.add(value.get(0).getUv());
                } else {
                    pv.add(0);
                    uv.add(0);
                }
            });
        }
        map.put("pv", pv);
        map.put("uv", uv);
        return map;
    }

    @Override
    public Map<String, List<Integer>> getPvOrUvByTime(TimeDto dto) {
        List<String> days = MyConstants.getDateList(dto.getStartTime(), dto.getEndTime());
        LambdaQueryWrapper<Flow> wrapper = new LambdaQueryWrapper<>();
        wrapper.gt(Flow::getCreateTime, dto.getStartTime());
        wrapper.lt(Flow::getCreateTime, dto.getEndTime());
        List<Flow> flowList = flowService.list(wrapper);
        Map<String, List<Integer>> map = new HashMap<>(8);
        List<Integer> pv = new ArrayList<>();
        List<Integer> uv = new ArrayList<>();
        if (flowList.isEmpty()) {
            for (int i = 0; i < days.size(); i++) {
                pv.add(0);
                uv.add(0);
            }
        } else {
            DateFormat dateFormat = BaseConstants.SDF1;
            Map<Object, List<Flow>> ordersPerHour = flowList.stream()
                    .collect(Collectors.groupingBy(order -> dateFormat.format(order.getCreateTime())));
            for (String day : days) {
                int countP = 0;
                int countV = 0;
                boolean a = false;
                for (Object key : ordersPerHour.keySet()) {
                    if (key.equals(day)) {
                        for (int j = 0; j < ordersPerHour.get(key).size(); j++) {
                            countP += ordersPerHour.get(key).get(j).getPv();
                            countV += ordersPerHour.get(key).get(j).getUv();
                        }
                        a = true;
                        break;
                    }
                }
                if (a) {
                    pv.add(countP);
                    uv.add(countV);
                } else {
                    pv.add(0);
                    uv.add(0);
                }
            }
        }
        map.put("pv", pv);
        map.put("uv", uv);
        return map;
    }

    @Override
    public WebUserAnalysis getWebUserAnalysis(TimeDto dto) {
        WebUserAnalysis vo = baseMapper.getWebUserAnalysis(dto.getStartTime(),dto.getEndTime());
        List<Date> dateList = new ArrayList<>();
        for(int i=0;i<=MyConstants.daysBetween(dto.getStartTime(),dto.getEndTime());i++) {
            Calendar d = Calendar.getInstance();
            d.setTime(dto.getStartTime());
            d.add(Calendar.DAY_OF_MONTH,i);
            dateList.add(d.getTime());
        }
        List<WebUserAnalysis> dList = baseMapper.getWebUserAnalysisList(dateList);
        List<Integer> addList = new ArrayList<>();
        List<Integer> activeList = new ArrayList<>();
        List<Integer> payList = new ArrayList<>();
        List<Integer> allAddList = new ArrayList<>();
        List<Integer> allPayList = new ArrayList<>();
        for(WebUserAnalysis v:dList) {
            addList.add(v.getAddCount());
            activeList.add(v.getActiveCount());
            payList.add(v.getPayCount());
            allAddList.add(v.getAllCount());
            allPayList.add(v.getAllPayCount());
        }
        Map<String, List<Integer>> map = new HashMap<>();
        map.put("add", addList);
        map.put("active", activeList);
        map.put("pay", payList);
        map.put("allAdd", allAddList);
        map.put("allPay", allPayList);
        vo.setMap(map);
        return vo;
    }

    @Override
    public TransactionVo getTransactionAnalysis(TimeDto dto) {
        TransactionVo vo = baseMapper.getTransactionAnalysis(dto.getStartTime(),dto.getEndTime());
        List<Date> dateList = new ArrayList<>();
        for(int i=0;i<=MyConstants.daysBetween(dto.getStartTime(),dto.getEndTime());i++) {
            Calendar d = Calendar.getInstance();
            d.setTime(dto.getStartTime());
            d.add(Calendar.DAY_OF_MONTH,i);
            dateList.add(d.getTime());
        }
        List<TranAnalysisLineVo> paymentTrendsVo = baseMapper.getPaymentTrends(dateList);
        List<Object> paymentAmount = new ArrayList<>();
        List<Object> paymentCount = new ArrayList<>();
        for(TranAnalysisLineVo v:paymentTrendsVo) {
            paymentAmount.add(v.getAmount());
            paymentCount.add(v.getUserCount());
        }
        List<List<Object>> paymentTrends = new ArrayList<>();
        paymentTrends.add(paymentAmount);
        paymentTrends.add(paymentCount);
        List<TranAnalysisLineVo> zhlList = baseMapper.getConversionRateTrend(dateList);
        List<BigDecimal> xd = new ArrayList<>();
        List<BigDecimal> fk = new ArrayList<>();
        List<BigDecimal> cj = new ArrayList<>();
        for (TranAnalysisLineVo v:zhlList) {
            xd.add(v.getXdzhl());
            fk.add(v.getFkzhl());
            cj.add(v.getCjzhl());
        }
        List<List<BigDecimal>> conversionRateTrends = new ArrayList<>();
        conversionRateTrends.add(xd);
        conversionRateTrends.add(fk);
        conversionRateTrends.add(cj);
        List<TranAnalysisLineVo> szList = baseMapper.getRevenueExpenditureStatement(dateList);
        List<BigDecimal> sr = new ArrayList<>();
        List<BigDecimal> zc = new ArrayList<>();
        for(TranAnalysisLineVo v:szList) {
            sr.add(v.getSr());
            zc.add(v.getZc());
        }
        List<List<BigDecimal>> revenueAndExpenditureStatementTrends = new ArrayList<>();
        revenueAndExpenditureStatementTrends.add(sr);
        revenueAndExpenditureStatementTrends.add(zc);
        vo.setPaymentTrends(paymentTrends);
        vo.setConversionRateTrends(conversionRateTrends);
        vo.setRevenueAndExpenditureStatementTrends(revenueAndExpenditureStatementTrends);
        return vo;
    }

    private List<GoodsQuotationVo> beanToVo(List<OrderGoods> orderGoods) {
        List<GoodsQuotationVo> voList = new ArrayList<>();
        orderGoods.forEach(goods -> {
            GoodsQuotationVo vo = new GoodsQuotationVo();
            BeanUtils.copyProperties(goods, vo);
            voList.add(vo);
        });
        return voList;
    }
}
